package com.vdong.common.oauth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.common.collect.Maps;

import com.vdong.common.oauth.common.enums.SmsConstant;
import com.vdong.common.oauth.common.exception.WebBizException;
import com.vdong.common.oauth.service.SmsSendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @program: kpay-basics-srv
 * @description: 短信发送实现
 * @author: Mr.Zhang
 * @create: 2018-06-07 13:57
 **/
@Component
public class SmsSendServiceImpl implements SmsSendService {
    private static Logger logger = LoggerFactory.getLogger(SmsSendServiceImpl.class);

    @Override
    public String sendVerifyCode(String mobile, SmsConstant.VerifyCodeType verifyCodeType, String code, String product) {
        //短信API产品名称（短信产品名固定，无需修改）
        product = "Dysmsapi";
        //短信API产品域名（接口地址固定，无需修改）
        final String domain = "dysmsapi.aliyuncs.com";
        //你的accessKeyId
        final String accessKeyId = "eltxS5hSYeIBXzp2";
        //你的accessKeySecret
        final String accessKeySecret = "dJsgW1ZKtIEvPQHSLvrGySlt4bOtWa";
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        Map<String, String> keyMap = Maps.newHashMap();
        keyMap.put("code", code);
        switch (verifyCodeType) {
            case LOGIN:
            case PWD:
                keyMap.put("product", product);
                break;
            default:
                keyMap.put("product", product);
        }
        String json = JSON.toJSONString(keyMap);
        //组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(mobile);
        request.setSignName(SmsConstant.SIGN_NAME);
        request.setTemplateCode(verifyCodeType.getTempCode());
        request.setTemplateParam(json);
        SendSmsResponse sendSmsResponse;
        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
            logger.info("mobile:{} , sendSmsResponse:{}", mobile, JSON.toJSONString(sendSmsResponse));
        } catch (ClientException e) {
            e.printStackTrace();
            throw new WebBizException(e.getMessage());
        }
        return code;
    }

    @Override
    @Async
    public String sendGeneralMessage(String mobile, SmsConstant.SendGeneralSmsType sendGeneralSmsType, String paramsJson) {
        //短信API产品名称（短信产品名固定，无需修改）
        String product = "Dysmsapi";
        //短信API产品域名（接口地址固定，无需修改）
        final String domain = "dysmsapi.aliyuncs.com";
        //你的accessKeyId
        final String accessKeyId = "eltxS5hSYeIBXzp2";
        //你的accessKeySecret
        final String accessKeySecret = "dJsgW1ZKtIEvPQHSLvrGySlt4bOtWa";
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(mobile);
        request.setSignName(SmsConstant.SIGN_NAME);
        request.setTemplateCode(sendGeneralSmsType.getTempCode());
        request.setTemplateParam(paramsJson);
        SendSmsResponse sendSmsResponse;
        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
            logger.info("mobile:{} , sendSmsResponse:{}", mobile, JSON.toJSONString(sendSmsResponse));
        } catch (ClientException e) {
            e.printStackTrace();
            throw new WebBizException(e.getMessage());
        }
        return paramsJson;
    }
}
